home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / pt20pc.zip / AUXCOM.C < prev    next >
C/C++ Source or Header  |  1991-02-04  |  14KB  |  572 lines

  1. #include "pt.h"
  2. #include "io.h"
  3. #include "string.h"
  4. #include "stdlib.h"
  5. #include "direct.h"
  6.  
  7. int pascal
  8. /* XTAG:quitPoint */
  9. quitPoint(fn, noSave)
  10.     int fn, noSave;
  11. {
  12.     extern unsigned char msgBuffer[];
  13.     extern struct window *windowList;
  14.     extern struct window *hiddenList;
  15.     extern struct openFile *files;
  16.     extern int i43lines;
  17. #ifdef OVERLAYS
  18.     extern int overlayState;
  19. #endif
  20.     extern unsigned char startDirectory[];
  21.     extern unsigned char startDrive;
  22.     extern struct openFile *files;
  23.  
  24.     register struct window *w2;
  25.     register int j;
  26.     unsigned char *fileName;
  27.     int i, n;
  28.     int fid;
  29.  
  30.     /* create (or truncate) the last state file */
  31.     setDefaultDrive(startDrive);
  32.     chdir(startDirectory);
  33.     fid = creatls("pt.las", 0);
  34.     if( fid >= 0 ) {
  35.         sprintf(msgBuffer, "%d \r\n", i43lines);
  36.         writels(fid, msgBuffer, strlen(msgBuffer));
  37.     }
  38.     if( !noSave )
  39.         goto noChanges;
  40.     n = 0;
  41.     w2 = windowList;
  42.     while( 1 ) {
  43.         if( w2 == NULL ) {
  44.             /* gone through both windowList and hiddenList? */
  45.             if( n == 1 )
  46.                 break;    /* ifm we are done */
  47.             /* if not do hiddenList */
  48.             n = 1;    /* remember that we already did windowList */
  49.             w2 = hiddenList;
  50.             continue;
  51.         }
  52.         /* see if the file has been edited */
  53.         if( files[w2->fileId].isChanged )
  54.             goto areChanges;
  55.         w2 = w2->nextWindow;
  56.     }
  57.     goto noChanges;
  58. areChanges:
  59.     fileName = getInput(
  60.         "Are you sure you want to discard all changes?  ", "y", 1);
  61.     if( tolower(fileName[0]) != 'y' ) {
  62.         msg("Quit-Discard Edits cancelled", 1);
  63.         return 0;
  64.     }
  65. noChanges:
  66.     n = 0;
  67.     w2 = windowList;
  68.     while( 1 ) {
  69.         if( w2 == NULL ) {
  70.             /* gone through both windowList and hiddenList? */
  71.             if( n == 1 )
  72.                 break;    /* ifm we are done */
  73.             /* if not do hiddenList */
  74.             n = 1;    /* remember that we already did windowList */
  75.             w2 = hiddenList;
  76.             continue;
  77.         }
  78.         if( fn == FQUITASK )
  79.             j = 1;    /* ask about saving */
  80.         else
  81.             j = 0;    /* save without asking */
  82.         if( fn == FQUITNOSAVE )
  83.             j = 2;    /* do not ask or save */
  84.         i = closeWindow(w2, j, 0);
  85.         if( i == -1 ) {
  86.             msg("Window close failed, quit cancelled", 3);
  87.             goto noQuit;
  88.         }
  89.         /* record the state in the last state file */
  90.         if( fid >= 0 ) {
  91.             sprintf(msgBuffer, "%d %d %d %d %d %s \r\n",
  92.                 w2->row1, w2->col1, w2->row2, w2->col2,
  93.                 w2->numTopline, files[w2->fileId].origName);
  94.             writels(fid, msgBuffer, strlen(msgBuffer));
  95.         }
  96.         w2 = w2->nextWindow;
  97.     }
  98.     /* reset the mouse */
  99.     termMouse();
  100.     n = 1;
  101.     goto ret;
  102. noQuit:
  103.     n = 0;
  104. ret:
  105.     if( fid >= 0 )
  106.         closels(fid);
  107.     return n;
  108. }
  109.  
  110. unsigned char * pascal
  111. /* XTAG:getSelection */
  112. getSelection(s)
  113.     unsigned char *s;
  114. {
  115.     extern long selBegin, selEnd;
  116.     extern struct window *selWindow;
  117.  
  118.     register unsigned char *p;
  119.     long cp;
  120.     register int fid;
  121.     
  122.     p = s;
  123.     cp = selBegin;
  124.     fid = selWindow->fileId;
  125.     while( cp <= selEnd ) {
  126.         *p++ =  readChar(fid, cp++);
  127.         if( p-s >= 63 )
  128.             break;
  129.     }
  130.     *p = '\0';
  131.     return s;
  132. }
  133.  
  134. void pascal
  135. /* XTAG:matchChar */
  136. matchChar()
  137. {
  138.     extern struct window *selWindow;
  139.     extern long selBegin, selEnd;
  140.  
  141.     register unsigned char ch;
  142.     long cp, fSize;
  143.     unsigned char ch1, ch2;
  144.     int n, matchDirection, nLines;
  145.  
  146.     cp = selBegin;
  147.     ch = readChar(selWindow->fileId, cp);
  148.     switch( ch ) {
  149.     case '(': ch1 = '('; ch2 = ')'; matchDirection =  1; break;
  150.     case ')': ch1 = ')'; ch2 = '('; matchDirection = -1; break;
  151.     case '[': ch1 = '['; ch2 = ']'; matchDirection =  1; break;
  152.     case ']': ch1 = ']'; ch2 = '['; matchDirection = -1; break;
  153.     case '{': ch1 = '{'; ch2 = '}'; matchDirection =  1; break;
  154.     case '}': ch1 = '}'; ch2 = '{'; matchDirection = -1; break;
  155.     default:  ch1 = ' '; ch2 = ' '; matchDirection = -1; break;
  156.     }
  157.     if( ch1 == ' ' ) {
  158.         msg("Matching for (, ), [, ], {, and } only", 1);
  159.         return;
  160.     }
  161.     n = 1;    /* n==0 ==> we found the matching character */
  162.     nLines = 0;
  163.     fSize = fileSize(selWindow->fileId);
  164.     while( 1 ) {
  165.         cp += matchDirection;
  166.         ch = readChar(selWindow->fileId, cp);
  167.         if( ch == ch1 )
  168.             ++n;
  169.         else if( ch == ch2 )
  170.             --n;
  171.         else if( ch == '\n' )
  172.             ++nLines;
  173.         if( n == 0 || cp == 0 || cp >= fSize )
  174.             break;
  175.     }
  176.     if( n == 0 ) {
  177.         selBegin = selEnd = cp;
  178.         /* put the selection on the third line */
  179.         if( selBegin >= selWindow->posBotline 
  180.          || selBegin < selWindow->posTopline ) {
  181.             /* remember where we came from */
  182.             selWindow->rowLastline = selWindow->numTopline;
  183.             n = 3;
  184.             cp = prevLine(selWindow->fileId, selBegin, &n);
  185.             selWindow->posTopline = cp;
  186.             /* recalculate the line number by letting */
  187.             /* prevLine count as far back as it can */
  188.             n = 30000;
  189.             prevLine(selWindow->fileId, cp, &n);
  190.             selWindow->numTopline = n + 1;
  191.         }
  192.         redrawBox(selWindow->row1, selWindow->col1,
  193.             selWindow->row2, selWindow->col2);
  194.         updateScreen(selWindow->row1, selWindow->row2);
  195.     } else
  196.         msg("No matching character was found.", 1);
  197. }
  198.  
  199. int pascal
  200. /* XTAG:getUnixState */
  201. getUnixState(fileId)
  202.     int fileId;
  203. {
  204.     extern unsigned char msgBuffer[];
  205.     extern unsigned char textBuffer[];
  206.     extern int unixMode;
  207.  
  208.     long cp, limit;
  209.     int fileUnixMode;
  210.  
  211.     /* if unixMode is 0 or 1 then that+1 is the window's unixMode */
  212.     if( unixMode == 0 )
  213.         return 1;
  214.     else if( unixMode == 1 )
  215.         return 3;
  216.     /* otherwise, sample the first 300 characters */
  217.     /* if no CRs are found then assume it is a UNIX file */
  218.     /* if a CR is found, then it is a DOS file */
  219.     cp = 0L;
  220.     limit = fileSize(fileId) - 1;
  221.     if( limit > 300 )
  222.         limit = 300;
  223.     fileUnixMode = 3;
  224.     while( cp < limit ) {
  225.         if( readChar(fileId, cp) == '\r' ) {
  226.             fileUnixMode = 1;
  227.             break;
  228.         }
  229.         ++cp;
  230.     }
  231.     return fileUnixMode;
  232. }
  233.  
  234. int pascal
  235. /* XTAG:indentToShowSelection */
  236. /* CPC 4-26-88 */
  237. indentToShowSelection(selCol)
  238.     int selCol;    /* the column where the first character of */
  239.             /* the selection is */
  240. /* CPC 4-26-88 */
  241. {
  242.     extern unsigned char msgBuffer[];
  243.     extern struct window *selWindow;
  244.     extern long selBegin, selEnd;
  245.  
  246.     register struct window *rselWindow;
  247.     long longIndent;
  248.     int dummy, col, windowWidth, indent;
  249.     int originalIndent;
  250.  
  251.     rselWindow = selWindow;
  252.     originalIndent = rselWindow->indent;
  253.  
  254.     /* find the column the selection starts in */
  255. /* CPC 4-26-88 */
  256.     if( selCol == -1 )
  257.         (void)posToxy(rselWindow, selBegin, &dummy, &col);
  258.     else
  259.         col = selCol;
  260. /* CPC 4-26-88 */
  261.  
  262.     /* posToxy subtracts off the current indent so add it back in */
  263.     /* posToxy adds in rselWindow->col1 so subtract it off */
  264.     col += rselWindow->indent - rselWindow->col1;
  265.     windowWidth = rselWindow->col2 - rselWindow->col1 - 1;
  266.  
  267.     /* if the selection is right of the window, change the indent */
  268.     if( (col > (windowWidth+rselWindow->indent)) ) {
  269.         indent = col - (windowWidth>>1);
  270.         /* figure the column of the end of the selection */
  271.         /*     add window width / 2 to put it in the  */
  272.         /*    middle of the window */
  273.         longIndent = (selEnd - selBegin) + (long)indent;
  274.         if( longIndent < (long)col )
  275.             rselWindow->indent = (int)longIndent;
  276.         else
  277.             rselWindow->indent = indent;
  278.     } else if( (col < rselWindow->indent) ) {
  279.         /* if the selection is left of the window change the indent */
  280.         /* change the indent back to zero if this will still leave */
  281.         /* the beginning of the selection in the left half of the */
  282.         /* window */
  283.         indent = col - (windowWidth>>1);
  284.         if( indent < 0 )
  285.             indent = 0;
  286.         rselWindow->indent = indent;
  287.     }
  288.     /* indicate on return whether you actually changed the indent */
  289.     return (originalIndent != rselWindow->indent);
  290. }
  291.  
  292. void pascal
  293. /* XTAG:doGoSel */
  294. doGoSel(w)
  295.     struct window *w;
  296. {
  297.     extern struct window *selWindow;
  298.     extern long selBegin, selEnd;
  299.     extern int linesOverFind;
  300.     extern int debug;
  301.  
  302.     register struct window *rselWindow;
  303.     register int n;
  304.     int i, fid;
  305.     long cp, toCp;
  306.  
  307.     rselWindow = selWindow;
  308.  
  309.     /* remember where we came from */
  310.     w->rowLastline = w->numTopline;
  311.  
  312.     /* n is the number of lines to move */
  313.     fid = rselWindow->fileId;
  314.     cp = rselWindow->posTopline;
  315.     i = linesOverFind;
  316.     /* find the number of lines in the window */
  317.     i = rselWindow->row2 - rselWindow->row1 - 2;
  318.     if( linesOverFind > i )
  319.         /* if linesOverFind would place it outside the */
  320.         /* window then put it in the middle